package com.sangmo.fts.basic.model;

import com.sangmo.boot.framework.data.entity.general.SoftDeleteEntity;
import com.sangmo.boot.framework.data.json.meta.ExtView;
import com.sangmo.boot.framework.data.json.meta.One;
import com.sangmo.boot.framework.plugin.dataview.def.meta.DataField;
import com.sangmo.boot.framework.plugin.dataview.def.meta.DataView;
import com.sangmo.fts.common.IConstants;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Where;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Entity
@Table(name = "company")
@DataView
@ExtView
@SQLDelete(
        sql = "UPDATE #{entityName} SET deleted = CURRENT_TIMESTAMP WHERE id = ?"
)
@SQLDeleteAll(
        sql = "UPDATE #{entityName} SET deleted = CURRENT_TIMESTAMP WHERE id = ?"
)
@Where(
        clause = "deleted = 0"
)
public class Company extends SoftDeleteEntity {

    @DataField(value = IConstants.DATA_TYPE.STRING_50,required = true, index = true, unique = true)
    private String name;

    @DataField(value = IConstants.DATA_TYPE.STRING_50,required = true, index = true, unique = true)
    private String code;

    @DataField(value = IConstants.COMPANY_DATA_TYPE.COMPANY_TYPE, required = true, index = true)
    private String type;

    @DataField(value = IConstants.DATA_TYPE.STRING_10, required = true)
    private String manager;

    @DataField(value = IConstants.DATA_TYPE.STRING_50, required = true)
    private String phone;

    private String registeredAddress;

    @One(value = "bank", target = Company.class, fieldset = {"id","name"},groups = {"Company.search","RpfWaybill.sellerSelection","FfWaybill.sellerSelection"})
    private Long bankId;

    private String bankNum;

    @DataField(value = IConstants.COMPANY_DATA_TYPE.COMPANY_CREDIT_RATING, required = true)
    private String creditRating;

    private boolean internal;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getManager() {
        return manager;
    }

    public void setManager(String manager) {
        this.manager = manager;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRegisteredAddress() {
        return registeredAddress;
    }

    public void setRegisteredAddress(String registeredAddress) {
        this.registeredAddress = registeredAddress;
    }

    public Long getBankId() {
        return bankId;
    }

    public void setBankId(Long bankId) {
        this.bankId = bankId;
    }

    public String getBankNum() {
        return bankNum;
    }

    public void setBankNum(String bankNum) {
        this.bankNum = bankNum;
    }

    public String getCreditRating() {
        return creditRating;
    }

    public void setCreditRating(String creditRating) {
        this.creditRating = creditRating;
    }

    public boolean isInternal() {
        return internal;
    }

    public void setInternal(boolean internal) {
        this.internal = internal;
    }

    public interface Type {
        String BUYER = "BUYER";
        String SELLER = "SELLER";
        String BANK = "BANK";
        String STORAGE = "STORAGE";
    }

    public interface CreditRating {
        String AAA = "AAA";
        String AA = "AA";
        String A = "A";
        String BBB = "BBB";
        String BB = "BB";
        String B = "B";
        String CCC = "CCC";
        String CC = "CC";
        String C = "C";
        String D = "D";
    }

    public static Map<String, BigDecimal> CreditRatingTypes = new HashMap<String, BigDecimal>()
    {
        {
            put("AAA", new BigDecimal("0.1"));
            put("AA", new BigDecimal("0.2"));
            put("A", new BigDecimal("0.3"));
            put("BBB", new BigDecimal("0.4"));
            put("BB", new BigDecimal("0.5"));
            put("B", new BigDecimal("0.6"));
            put("CCC", new BigDecimal("0.7"));
            put("CC", new BigDecimal("0.8"));
            put("C", new BigDecimal("0.9"));
        }
    };

    public static final Map<String, BigDecimal> rateMap = new HashMap<String, BigDecimal>(){
        {
            put("AAA-ONE_MONTH", new BigDecimal("0.06375"));
            put("AAA-THREE_MONTH", new BigDecimal("0.0663"));
            put("AAA-SIX_MONTH", new BigDecimal("0.068"));
            put("AA-ONE_MONTH", new BigDecimal("0.066"));
            put("AA-THREE_MONTH", new BigDecimal("0.06864"));
            put("AA-SIX_MONTH", new BigDecimal("0.0704"));
            put("A-ONE_MONTH", new BigDecimal("0.0675"));
            put("A-THREE_MONTH", new BigDecimal("0.0702"));
            put("A-SIX_MONTH", new BigDecimal("0.072"));
            put("BBB-ONE_MONTH", new BigDecimal("0.069"));
            put("BBB-THREE_MONTH", new BigDecimal("0.07176"));
            put("BBB-SIX_MONTH", new BigDecimal("0.0736"));
            put("BB-ONE_MONTH", new BigDecimal("0.072"));
            put("BB-THREE_MONTH", new BigDecimal("0.07488"));
            put("BB-SIX_MONTH", new BigDecimal("0.0768"));
            put("B-ONE_MONTH", new BigDecimal("0.075"));
            put("B-THREE_MONTH", new BigDecimal("0.078"));
            put("B-SIX_MONTH", new BigDecimal("0.08"));
            put("CCC-ONE_MONTH", new BigDecimal("0.07875"));
            put("CCC-THREE_MONTH", new BigDecimal("0.0819"));
            put("CCC-SIX_MONTH", new BigDecimal("0.084"));
            put("CC-ONE_MONTH", new BigDecimal("0.0825"));
            put("CC-THREE_MONTH", new BigDecimal("0.0858"));
            put("CC-SIX_MONTH", new BigDecimal("0.088"));
            put("C-ONE_MONTH", new BigDecimal("0.08625"));
            put("C-THREE_MONTH", new BigDecimal("0.0897"));
            put("C-SIX_MONTH", new BigDecimal("0.092"));
        }
    };
}
